/**
  * Tunnel effect made with stroked ellipses.
  * Demonstrates different dash pattern effects.
  *
  */

#include "game.h"

// for readability, the screen and texture sizes are hard-coded
const float SCREEN_WIDTH=640.0f;
const float SCREEN_HEIGHT=360.0f;
const int AMOUNT=30;

const VGfloat backgroundColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

struct DemoMode
{
    VGCapStyle cap;
    VGint dashPatternLength;
    const float *dashPattern;
};

const float dashPatternDot[]={ 0.0f, 12.0f };
const float dashPatternLine[]={ 15.0f, 5.0f };
const float dashPatternDotLine[]={ 15.0f, 10.0f, 0.0f, 10.0f, 0.0f, 10.0f, };

const DemoMode modes[]={
    { VG_CAP_BUTT, 0, NULL},
    { VG_CAP_ROUND, 2, dashPatternDot}, // dash length of 0.0 combined with VG_CAP_ROUND gives a round dot
    { VG_CAP_BUTT, 2, dashPatternLine},
    { VG_CAP_ROUND, 6, dashPatternDotLine},
};

int currentMode=0;

VGPaint paint;
VGPath ellipse;

/** walltime */
float t;

void game_init()
{
}

void game_destroy()
{
}

void game_prepare()
{
    vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);

    paint = vgCreatePaint();
    vgSetParameteri(paint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
    vgSetColor(paint, 0xFFFF00FF);
    vgSetPaint(paint, VG_STROKE_PATH);

    ellipse = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
    vguEllipse(ellipse, 0, 0, 100, 100);
}

void game_release()
{
    vgDestroyPath(ellipse);
    vgDestroyPaint(paint);
}

void game_update(float walltime)
{
    t=walltime*4.0f;
}

void game_render()
{
    // clear background using black
    vgSetfv(VG_CLEAR_COLOR, 4, backgroundColor);
    vgClear(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    // set line style here, Qt painter modifies stroke
    vgSetPaint(paint, VG_STROKE_PATH);
    vgSeti(VG_STROKE_CAP_STYLE, modes[currentMode].cap);
    vgSetfv(VG_STROKE_DASH_PATTERN, modes[currentMode].dashPatternLength, modes[currentMode].dashPattern);

    vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
    vgSeti(VG_BLEND_MODE, VG_BLEND_SCREEN);

    for(int i=0;i<AMOUNT;i++)
    {
        float j = (AMOUNT-i)+(1.0f-fmod(t,1.0f));

        int alpha=i*255/(AMOUNT-1);
        if(alpha>255) alpha=255;
        vgSetColor(paint, 0xe0d32200 + alpha);

        vgLoadIdentity();
        vgTranslate(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);

        float s=(0.00025f+1.0f/(j))*30.0f;
        vgSetf(VG_STROKE_LINE_WIDTH, s+1);
        vgScale(s,s);

        vgTranslate(cos(t*.63f+j*.356f)*25, sin(t*.833f+j*.417f)*20);

        vgDrawPath(ellipse, VG_STROKE_PATH);
    }

}

void game_event(int type, float x, float y, float z)
{
    if(type==TOUCH_PRESS)
    {
        currentMode = (currentMode+1) % (sizeof(modes)/sizeof(modes[0]));
    }

}
